package com.ensibs.nuitinfo2013;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.ensibs.nuitinfo2013.model.Collection;
import com.ensibs.nuitinfo2013.model.Topic;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ConsulationListeActivity extends Activity {

	private ListView listView;
	private Collection collection;
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.consultationliste);
        
        collection = Collection.getInstance();
 
        listView = (ListView) findViewById(R.id.consultation_listview);
 
        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        
        Iterator<Topic> allTopics = collection.iterator();
        Log.e("ConsulationListeActivity", "toto : " + allTopics.hasNext());
        while(allTopics.hasNext()) {
        	Log.e("ConsulationListeActivity", "topics");
        	
        	Topic topic = (Topic) allTopics.next();
        	map = new HashMap<String, String>();
        	map.put("id", Integer.toString(topic.getId()));
            map.put("titre", topic.getTitre());
            if(topic.isResolu()){
            	map.put("status", "résolu");
            }
            else{
            	map.put("status", "en cours"); 
            }
            listItem.add(map);
         }
        /*
        map = new HashMap<String, String>();
        map.put("titre", "Mac hjghd y zfuy yg  yfg h tf ug y ugyug y  g yghj hgb hgfhjbvjfg uyf yfyu u yfy hg");
        map.put("status", "Ceci est un mac");
        listItem.add(map);
  		*/
 
        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.affichageitem,
               new String[] {"titre", "status"}, new int[] {R.id.consultation_titre, R.id.consultation_description});
 
        listView.setAdapter(mSchedule);
 
        listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
        	@SuppressWarnings("unchecked")
         	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        		HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        		
        		Intent mIntent = new Intent(ConsulationListeActivity.this, ConsulationActivity.class); 
        		Bundle mBundle = new Bundle();
        		Topic topic = Collection.getTopicById(Integer.parseInt(map.get("id")));
        		mBundle.putSerializable("topic", topic);
        		mIntent.putExtras(mBundle);
        		startActivity(mIntent);
        		
        		/*AlertDialog.Builder adb = new AlertDialog.Builder(ConsulationListeActivity.this);
        		adb.setTitle("Sélection Item");
        		adb.setMessage("Votre choix : "+map.get("titre"));
        		adb.setPositiveButton("Ok", null);
        		adb.show();*/
        	}	
         });
    }
}
